﻿using System.Collections.Generic;

namespace Red.Launcher.ViewModels.Workspaces.Database
{
    public class GroupToClientSpecs : Record
    {
        #region Inherited Properties

        public override Metadata Metadata { get { return METADATA; } }
        public override string Icon { get { return "appbar_group"; } }
        public override IEnumerable<object> Values { get { return new List<object>() { GroupId }; } }

        #endregion Inherited Properties

        #region Public Properties

        public static Metadata METADATA { get; private set; }

        public int GroupId { get; set; }

        #endregion Public Properties

        #region C-Tor

        static GroupToClientSpecs()
        {
            METADATA = new Metadata()
            {
                Table = "GroupToClientSpecs",
                Keys = new List<string>() { AssignedUserGroup.METADATA.ForeignKey },
                CreateDefault = () => { return null; },
                CreateFromDatabaseRow = ( Dictionary<string, object> data ) =>
                {
                    return new GroupToClientSpecs()
                    {
                        Id = (int)data[ "gtcs_id" ],
                        GroupId = (int)data[ AssignedUserGroup.METADATA.ForeignKey ],
                        Name = (string)data[ "name" ]
                    };
                },
                GenerateSelectSql = GenerateSelectSql
            };
        }

        public GroupToClientSpecs()
        {
        }

        public GroupToClientSpecs( AssignedUserGroup group )
        {
            GroupId = group.Id;
            Name = group.Name;
        }

        #endregion C-Tor

        #region Private Methods

        private static Sql.Select GenerateSelectSql( Record parent )
        {
            Sql.Select.Table source = Sql.Select.CreateTable( AssignedUserGroup.METADATA.Table, "aug" );

            Sql.Select query = new Sql.Select();
            query.Sources.Add( source );

            Sql.IAlias joinSource = query.AddStandardChildJoin( AssignedUserGroup.METADATA, GroupToClientSpecs.METADATA, source, P4ClientSpecification.METADATA, parent.Id );

            query.AddColumn( "id", joinSource, "gtcs_id" );
            query.AddColumn( "name", source );
            query.AddColumn( AssignedUserGroup.METADATA.ForeignKey, joinSource );

            return query;
        }

        #endregion Private Methods

        #region Comparison

        public override bool Equals( object obj )
        {
            if ( obj == null )
                return false;

            Record record = obj as Record;
            if ( record == null )
                return false;

            if ( ( GroupId == record.Id ) && ( Name == record.Name ) )
                return true;

            GroupToClientSpecs other = obj as GroupToClientSpecs;
            return Equals( other );
        }

        public bool Equals( GroupToClientSpecs other )
        {
            if ( other == null )
                return false;

            return ( GroupId == other.GroupId ) && ( Name == other.Name );
        }

        public override int GetHashCode()
        {
            return GroupId ^ Name.GetHashCode();
        }

        #endregion Comparison

    }
}
